/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.fedoraaccess.rest;

import cz.mzk.kramerius.fedoraaccess.FedoraServer;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
class PurgeRelationshipOperation extends AbstractOperation {

    private static final Logger logger = Logger.getLogger(PurgeObjectOperation.class.getName());

    public PurgeRelationshipOperation(FedoraServer fedoraServer) {
        super(new HttpConnector(fedoraServer, HttpMethod.DELETE));
    }

    public void run(String subject, String predicate, String object) {
        String request = buildRequest(subject, predicate, object);
        logger.log(Level.FINE, "request: ''{0}''", request);
        try {
            connector.processRequestWithStringResult(request);
        } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }

    private String buildRequest(String subjectPid, String predicate, String objectPid) {
        StringBuilder builder = new StringBuilder();
        builder.append(getBaseUrl());
        builder.append("/objects/").append(subjectPid);
        builder.append("/relationships/new?");
        String subject = encodeAttribute("info:fedora/".concat(subjectPid));
        builder.append("subject=").append(subject);
        builder.append("&predicate=");
        builder.append(encodeAttribute(predicate));
        builder.append("&object=");
        builder.append(encodeAttribute(objectPid));
        builder.append("&isLiteral=true");
        return builder.toString();
    }
}
